// pages/Allphoto/photoList/photoList.js
var utils = require("../../../utils/utils.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    preCode: '',
    list: [],
    firstOpen:0
  },
  getIndex() {
    var that = this;
    utils.request('Order/secondService', {
      pCode: that.data.preCode
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 1) {
        that.setData({
          list: res.data.data
        })
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  //项目跳转
  goItemUrl(e) {
    if(!utils.is_define(wx.getStorageSync('machineQrCode'))){
      utils.toast('请先选择设备')
      return false;
    }
    var that = this;
    var code = e.currentTarget.dataset.code;
    var status = e.currentTarget.dataset.status;
    if (status == '-1') {
      utils.toast('当前打印机不支持此方式');
      return false;
    }
    app.get_local('AccToken').then(res => {
      if (utils.is_define(res)) {
            app.isSupport(code).then(res => {
              if (res == -1) {
                utils.toast("当前打印机不支持此方式")
                return false;
              } else {
                if(code=='13623180'||code=='71167317'){
                  utils.toast('专区正在建设中') 
                  return false;
                }
                wx.showActionSheet({
                  itemList: ['相册', '拍照', '从QQ会话选择'],
                  success(res) {
                    if (res.tapIndex == 0) {
                      that.getPhoto('album', code);
                    }
                    if (res.tapIndex == 1) {
                      that.getPhoto('camera', code);
                    }
                    if (res.tapIndex == 2) {
                      that.getPhoto1(code);
                    }
                  },
                  fail(res) {
                    
                  }
                })
              }
            })
      } else {
        wx.navigateTo({
          url: '../my/login/login',
        })
      }
    })
  },
  //拍照调取相册
  getPhoto(type, code) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: [type],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0];
        if(code=='30177105'){
          wx.navigateTo({
            url: '/pages/print/photoEdit/photoEdit?&w=240&h=360&img=' + tempFilePaths + '&code=' + code,
          })
        }else{
          wx.navigateTo({
            url: '/pages/print/photoEdit/photoEdit?&w=240&h=360&img=' + tempFilePaths + '&code=' + code,
          })
        }
      }
    })
  },
  getPhoto1(code) {
    wx.chooseMessageFile({
      count: 1,
      type: 'image',
      success(res) {
        if(code=='30177105'){
          wx.navigateTo({
            url: '../../print/photoEdit/photoEdit?&w=240&h=360&img=' + res.tempFiles[0].path + '&code=' + code,
          })
        }else{
          wx.navigateTo({
            url: '../../print/photoEdit/photoEdit?&w=240&h=360&img=' + res.tempFiles[0].path + '&code=' + code,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      preCode: options.code
    })
    this.getIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
var that = this;
    if (that.data.firstOpen == 1) {
      that.getIndex();
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getIndex();
    setTimeout(function(){
      wx.stopPullDownRefresh()
    },1500)
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})